//
//  CornerSelectionView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/10.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class CornerSelectionView: UIView, ImageFactory, AddCornerUIRectCorner {
    
    private let buttonHeight: CGFloat = 35
    private let topView = UIView()
    private var tapIndex: ((NSInteger) -> Void)?
    private var beforButton: UIButton?
    private var buttenTitles: [String] = []
    
    func show(titles: [String], tapIndex: @escaping (NSInteger) -> Void) {
        buttenTitles = titles
        setUI()
        self.tapIndex = tapIndex
    }
    
    private func setUI() {
        self.layoutIfNeeded()
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 0.1) {
            DispatchQueue.main.async {
                self.addCornerWithView(aView: self, aCorners: [.topLeft, .topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
                self.topView.frame = CGRect(x: 0, y: 0, width: self.width, height: self.height)
                self.topView.backgroundColor = UIColor(hex: 0xC4C4C4)
                self.addSubview(self.topView)
                
                for (index, title) in self.buttenTitles.enumerated() {
                    let button = UIButton(type: .custom)
                    let buttonWidth: CGFloat = self.width / CGFloat(self.buttenTitles.count)
                    button.frame = CGRect(x: CGFloat(index) * buttonWidth, y: 0, width: buttonWidth, height: self.height)
                    button.tag = index
                    button.addTarget(self, action: #selector(self.selectTap(_ :)), for: .touchUpInside)
                    self.addCornerWithView(aView: button, aCorners: [.topLeft, .topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
                    button.adjustsImageWhenDisabled = false
                    button.adjustsImageWhenHighlighted = false;
                  let image = self.createImage(color: UIColor.white, height: self.height)?.withRenderingMode(.alwaysOriginal)
                    button.setBackgroundImage(image, for: .selected)
                    button.setBackgroundImage(image, for: .highlighted)
                    button.setTitle(title, for: .normal)
                    button.setTitleColor(ONColor.searchScreenTitleNormalColor, for: .normal)
                    button.setTitleColor(UIColor.black, for: .selected)
                    button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
                  
                    if index == 0 {
                        self.beforButton = button
                        button.isSelected = true
                    }
                    
                    self.topView.addSubview(button)
                }
            }
        }
    }
    
    @objc func selectTap(_ btn: UIButton) {
        btn.isSelected = true
        if let button = self.beforButton {
            if button != btn {
                button.isSelected = false
            }
        }
        self.beforButton = btn
        tapIndex?(btn.tag)
    }
    
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
}
